from django.core.management import setup_environ
import tigeralbum.settings
setup_environ(tigeralbum.settings)

import sys
from django.core.management import setup_environ
from django.db import connection
from hashlib import sha1

from tigeralbum.main.models import Photo
from tigeralbum.util import thisdirpath

sys.path.append(thisdirpath('', tigeralbum.__file__))

cursor = connection.cursor()
cursor.execute("""
ALTER TABLE main_photo
ADD permalink varchar(8) NOT NULL DEFAULT '00000000'
""")

for photo in Photo.objects.all():
    h = sha1()
    h.update(str(photo.id))
    h.update(str(photo.photo.name))
    photo.permalink = h.hexdigest()[0:8]
    photo.save()
